/*
 * MysqlBaseDB.java
 * 
 * Created by demory on Sep 19, 2009, 10:58:27 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fpdev.core.data;

import java.sql.DriverManager;
import java.sql.SQLException;
import org.fpdev.core.DataPackage;
import org.fpdev.core.FPEngine;

/**
 * MySQL-based implementation of CoreDB.
 *
 * @author demory
 */
public class MysqlCoreDB extends AbstractCoreDB {

  public MysqlCoreDB(FPEngine engine, DataPackage dataPkg, String user, String pw) {
    super(engine, dataPkg, user, pw);
  }

  @Override
  public void init() throws SQLException {
    String url = "jdbc:mysql://localhost/";

    System.out.println("user="+user_);
    System.out.println("pw="+pw_);
    try {
      conn_ = DriverManager.getConnection(url + dataPkg_.getName() + "?autoReconnect=true", user_, pw_);
      System.out.println("Connected to existing MySQL database " +dataPkg_.getName());
      initialized_ = true;
    } catch (SQLException e) {
      //initialized_ = createDB(url);
      e.printStackTrace();
    }
  }


  @Override
  public String jdbcClassname() {
    return "com.mysql.jdbc.Driver";
  }

  @Override
  public void close() {
    try {
      System.out.println("Closing MySQL BaseDB..");
      conn_.close();
    } catch (Throwable e) {
      System.out.println("exception thrown:");
      e.printStackTrace();
    }
  }

}
